{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Sampling Responses from Claude Beyond the Max Tokens Limit"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "5mLvQAF8IXpb"
   },
   "source": [
    "This notebook illustrates how to get Claude to give responses longer than the maximum value of the max_tokens parameter by using a prefill with the content of the previous message."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "rYhFpqG3FkCh"
   },
   "outputs": [],
   "source": [
    "%%capture\n",
    "!pip install anthropic"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "Z7WGWajWIk4N"
   },
   "source": [
    "First, we'll prompt Claude by asking it to write something longer than 4096 tokens."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "DoqWes2gFFwM",
    "outputId": "4534e59c-ce93-4bd8-9ad6-1ab762386622"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[TextBlock(text=\"<story_1>\\n1. Once upon a time, in the vast expanse of the African savanna, there lived a magnificent lion named Mufasa. 2. His golden mane rippled in the warm breeze, and his piercing amber eyes commanded respect from all who laid eyes upon him. 3. Mufasa was the king of the Pride Lands, a realm teeming with diverse wildlife and lush grasslands.\\n\\n4. As the sun rose each morning, Mufasa would take his daily patrol, surveying his territory with a vigilant gaze. 5. He knew every inch of the Pride Lands, from the towering acacia trees to the winding rivers that sustained life. 6. His powerful roar echoed across the plains, a reminder to all that he was the undisputed ruler of this domain.\\n\\n7. One day, while leading his pride on a hunt, Mufasa noticed a group of hyenas lurking in the shadows. 8. These scavengers were known for their cunning and ruthlessness, and they posed a constant threat to the delicate balance of the Pride Lands. 9. With a flick of his tail, Mufasa signaled his lionesses to remain alert, ready to defend their territory at any cost.\\n\\n10. As the days passed, the hyenas grew bolder, encroaching upon the pride's hunting grounds and terrorizing the other animals. 11. Mufasa knew that he had to take decisive action to protect his kingdom. 12. Gathering his strength, he let out a thunderous roar that shook the very foundations of the savanna, a warning to the hyenas to stay away.\\n\\n13. The hyenas, sensing Mufasa's unwavering determination, retreated for a time, but they were far from defeated. 14. They hatched a devious plan to strike at the heart of the pride, targeting Mufasa's beloved son and heir, Simba. 15. In a harrowing turn of events, Simba found himself caught in a treacherous stampede orchestrated by the hyenas.\\n\\n16. Mufasa, risking his own life, leapt into the chaos to rescue his son. 17. With unparalleled courage and strength, he fought against the raging herd, shielding Simba from harm. 18. But in a tragic twist of fate, Mufasa was swept away by the relentless stampede, his life tragically cut short.\\n\\n19. Simba, devastated by the loss of his father, fled the Pride Lands, leaving the kingdom vulnerable to the machinations of the hyenas and their sinister allies. 20. Years passed, and Mufasa's legacy seemed to fade into the distant memory of the pride.\\n\\n21. However, Mufasa's spirit lived on, guiding Simba from the heavens above. 22. As Simba grew into a formidable lion, he recognized the importance of reclaiming his rightful place as the king of the Pride Lands. 23. With the help of his friends and the wisdom imparted by his father's spirit, Simba embarked on a perilous journey to confront the usurpers and restore balance to the kingdom.\\n\\n24. In an epic battle against the hyenas and their leader, Simba emerged victorious, honoring his father's memory and taking his rightful place as the new king. 25. As he stood atop Pride Rock, his mane catching the golden rays of the setting sun, Simba vowed to uphold the legacy of Mufasa, ruling with wisdom, courage, and unwavering devotion to the Circle of Life.\\n\\n26. From that day forward, the Pride Lands flourished under Simba's just and benevolent reign, and the spirit of Mufasa lived on, guiding future generations of lions to protect and cherish the delicate balance of nature. 27. The legend of the brave and noble Mufasa became etched into the hearts of all who called the Pride Lands their home, a testament to the enduring power of a father's love and sacrifice.\\n</story_1>\\n\\n<story_2>\\n1. Deep within the lush rainforests of the Amazon, a family of spider monkeys swung gracefully from branch to branch, their long tails providing perfect balance. 2. Among them was Chico, a mischievous and curious young monkey whose insatiable appetite for adventure often landed him in precarious situations.\\n\\n3. One morning, as the family foraged for succulent fruits and tender leaves, Chico's attention was drawn to a peculiar sight. 4. A group of brightly colored macaws had descended upon a nearby tree, their vibrant plumage a stark contrast against the emerald canopy. 5. Chico had never seen such magnificent creatures before, and his curiosity got the better of him.\\n\\n6. Ignoring the warnings of his elders, Chico stealthily approached the macaws, his agile movements barely disturbing the foliage around him. 7. As he drew closer, the macaws took notice of the inquisitive monkey and let out a cacophony of squawks and screeches, their wings flapping furiously.\\n\\n8. Undeterred, Chico continued his approach, his mischievous grin widening with every step. 9. However, his bold actions did not go unnoticed by the macaws' leader, a magnificent scarlet macaw with a regal crest atop his head. 10. With a deafening screech, the leader issued a warning, causing the rest of the flock to take flight in a whirlwind of vibrant colors.\\n\\n11. Chico, enthralled by the spectacle, leapt from branch to branch, determined to keep up with the macaws. 12. His family watched in horror as he disappeared into the dense foliage, their calls for him to return lost in the symphony of the rainforest.\\n\\n13. For hours, Chico followed the macaws, his nimble form weaving through the intricate maze of vines and branches. 14. The macaws, sensing a potential threat, led him deeper and deeper into the heart of the rainforest, their flight seemingly effortless.\\n\\n15. As night fell, Chico found himself utterly lost, surrounded by unfamiliar sights and sounds. 16. The calls of his family had long faded, replaced by the eerie hoots of owls and the rustling of unseen creatures lurking in the shadows.\\n\\n17. Exhausted and disoriented, Chico curled up on a sturdy branch, his eyes wide with fear. 18. The night passed slowly, every rustle of leaves sending shivers down his spine. 19. As the first rays of dawn filtered through the canopy, Chico realized the gravity of his situation – he was alone, far from the safety of his family.\\n\\n20. Mustering his courage, Chico set out to retrace his steps, hoping to find familiar landmarks that would guide him home. 21. However, the rainforest proved to be a treacherous labyrinth, each path seeming to lead him further astray.\\n\\n22. Days turned into weeks, and Chico's once vibrant coat grew matted and dull. 23. He learned to forage for sustenance, relying on his sharp senses and agility to survive in the unforgiving wilderness.\\n\\n24. Just when all hope seemed lost, Chico's keen ears picked up a familiar sound – the distinct calls of his family. 25. With renewed determination, he followed the calls, his heart pounding with anticipation.\\n\\n26. Finally, after what seemed like an eternity, Chico emerged from the dense foliage and was greeted by the joyous cries of his kin. 27. Tears of relief streamed down his face as he embraced his family, vowing never again to stray too far from their loving embrace.\\n\\n28. From that day forward, Chico's adventurous spirit was tempered by wisdom and caution. 29. He understood the importance of staying close to his family, for in the vast and unpredictable rainforest, their unity was their greatest strength. 30. And whenever he heard the distant squawks of macaws, Chico would smile, remembering the fateful encounter that had taught him a valuable lesson about the wonders and perils of the Amazon jungle.\\n</story_2>\\n\\n<story_3>\\n1. In the icy depths of the Arctic Ocean, a majestic polar bear named Nanuq prowled the frozen landscape, her powerful paws leaving imprints in the crisp snow. 2. She was the undisputed queen of this unforgiving realm, a master hunter whose keen senses and unwavering determination ensured her survival in the harshest of conditions.\\n\\n3. As the long Arctic winter dragged on, Nanuq's instincts drove her to seek out the best hunting grounds, where the seals congregated in abundance. 4. With each passing day, her hunger grew more insatiable, fueling her relentless pursuit of prey.\\n\\n5. One frigid morning, Nanuq caught the scent of a nearby seal colony, her nostrils flaring with anticipation. 6. Silently, she stalked her way across the vast expanse of ice, her movements fluid and calculated. 7. The seals, oblivious to the approaching danger, basked in the pale Arctic sun, their sleek bodies glistening against the snow.\\n\\n8. Nanuq's powerful jaws began to water as she drew closer, her muscles coiled like springs, ready to strike. 9. In a blur of white fur and razor-sharp claws, she pounced upon her unsuspecting prey, her massive paws pinning the struggling seal to the ice.\\n\\n10. As the hunt unfolded, a curious sight caught Nanuq's eye – a human vessel, cutting through the icy waters with ease. 11. She had encountered humans before, fearless explorers who ventured into her realm with awe and respect. 12. Yet, this time, something felt different, a sense of foreboding that sent a chill down her spine.\\n\\n13. In the days that followed, Nanuq witnessed the human presence grow stronger, their machines and equipment encroaching upon her hunting grounds. 14. The once-pristine ice began to fracture and melt, disrupting the delicate balance of the Arctic ecosystem.\\n\\n15. As the ice retreated, so too did the seals, their traditional breeding and resting grounds disappearing before Nanuq's eyes. 16. Desperate to find sustenance, she ventured farther and farther from her familiar territory, her journey becoming increasingly perilous.\\n\\n17. One fateful day, Nanuq found herself stranded on a rapidly melting ice floe, her powerful body weakened by hunger and exhaustion. 18. The relentless currents tossed her about, separating her from the few remaining seals in the area.\\n\\n19. As the sun dipped below the horizon, Nanuq let out a mournful roar, her cry echoing across the vast, empty expanse of water. 20. In that moment, she understood the grave threat facing her kind – the inexorable march of climate change, fueled by human activity, was slowly eroding the very foundations of her Arctic home.\\n\\n21. With a newfound determination, Nanuq embarked on a perilous journey, swimming for days on end, her powerful strokes cutting through the icy waters. 22. She sought out new hunting grounds, adapting to the ever-changing landscape, her resilience and strength pushing her forward.\\n\\n23. Along the way, she encountered other polar bears, their stories echoing her own struggles. 24. Together, they formed a formidable pack, united in their fight for survival against the forces of nature and humankind.\\n\\n25. As the years passed, Nanuq's legacy lived on through her offspring, each generation bearing witness to the rapid transformation of the Arctic. 26. Their once-frozen kingdom was now a vast, open ocean, dotted with shrinking ice floes and the remnants of a world that was slowly slipping away.\\n\\n27. Yet, even in the face of such adversity, the polar bears remained resolute, their unwavering spirit a testament to the resilience of nature itself. 28. And as Nanuq's descendants gazed out across the ever-changing Arctic, they carried within them the memory of their matriarch, a fierce and noble queen who fought to the very end to preserve the delicate balance of life in the great white north.\\n</story_3>\\n\\n<story_4>\\n1. In the heart of the lush Australian outback, a family of kangaroos grazed peacefully on the sun-drenched grasslands. 2. Among them was Kiki, a spirited young joey whose boundless energy and insatiable curiosity often led her into mischievous escapades.\\n\\n3. One sultry afternoon, as the kangaroo mob rested in the shade of a towering gum tree, Kiki's attention was captured by a strange, slithering creature. 4. Its iridescent scales glistened in the dappled sunlight, and its forked tongue flicked in and out, tasting the warm air.\\n\\n5. Ignoring the warnings of her elders, Kiki hopped closer, her large feet kicking up clouds of red dust. 6. The snake, sensing the presence of the inquisitive joey, coiled and hissed, its menacing fangs glinting in the sunlight.\\n\\n7. Undeterred, Kiki continued her approach, her black button nose twitching with excitement. 8. Just as she reached out a paw to touch the serpent, her mother intervened, scooping her up and bounding away to safety.\\n\\n9. Kiki's curiosity, however, remained unabated. 10. In the days that followed, she became obsessed with the enigmatic snake, spending hours watching it from a safe distance, studying its every move.\\n\\n11. As the seasons changed and the harsh Australian summer gave way to a cool autumn, Kiki's fascination only grew stronger. 12. She ventured farther and farther from her family, her explorations taking her deep into the rugged outback terrain.\\n\\n13. One crisp morning, Kiki found herself face-to-face with the snake once more, its hypnotic gaze holding her in place. 14. With a flick of its tongue, the serpent began to slither towards her, its powerful coils rippling across the dusty ground.\\n\\n15. Kiki's heart raced, her instincts torn between fight and flight. 16. Just as the snake lunged, a booming voice echoed through the bush, startling them both. 17. It was Kiki's father, a powerful buck whose imposing presence commanded respect.\\n\\n18. With a swift kick of his powerful hind legs, he sent the snake recoiling, its body coiling in defensive strikes. 19. Kiki watched in awe as her father stood his ground, his muscles rippling beneath his sleek coat.\\n\\n20. In that moment, Kiki understood the true nature of the outback – a land of both beauty and danger, where survival depended on respect for the delicate balance of nature.\\n\\n21. As the years passed, Kiki's adventurous spirit never waned, but her encounters with the snake had instilled in her a deeper appreciation for the power and resilience of the Australian wildlife.\\n\\n22. She became a skilled hunter and navigator, her powerful legs propelling her across the vast, sun-baked plains with ease. 23. Her keen senses alerted her to the slightest movements, allowing her to avoid potential threats and seek out the best grazing grounds.\\n\\n24. And whenever she caught a glimpse of that familiar serpentine form, Kiki would pause, her eyes filled with a mixture of fascination and respect. 25. For in the harsh and unforgiving outback, every creature played a vital role in the intricate web of life.\\n\\n26. As the seasons cycled and the kangaroo mob grew, Kiki passed on her hard-earned wisdom to the next generation, teaching them the importance of vigilance and respect for the land that sustained them.\\n\\n27. And on moonlit nights, when the outback came alive with the haunting calls of dingoes and the rustle of nocturnal creatures, Kiki would gaze up at the brilliant tapestry of stars, her heart swelling with gratitude for the magnificent world she called home.\\n</story_4>\\n\\n<story_5>\\n1. Deep within the lush, emerald canopy of the Amazon rainforest, a vibrant family of toucans made their home. 2. Among them was Zico, a mischievous and curious young toucan whose insatiable appetite for adventure often led him astray.\\n\\n3. One morning, as the family foraged for succulent fruits and juicy insects, Zico's attention was drawn to a peculiar sight. 4. A group of colorful macaws had descended upon a nearby tree, their vibrant plumage a stark contrast against the verdant foliage.\\n\\n5. Zico had never encountered such magnificent creatures before, and his curiosity piqued. 6. Ignoring the warnings of his elders, he took flight, his powerful wings carrying him closer to the macaws.\\n\\n7. As he drew nearer, the macaws took notice of the inquisitive toucan, letting out a cacophony of squawks and screeches. 8. Undeterred, Zico continued his approach, his distinctive beak bobbing with every flap of his wings.\\n\\n9. Suddenly, the leader of the macaw flock, a magnificent scarlet macaw with a regal crest, took to the air, its wings beating furiously. 10. With a deafening screech, the leader issued a warning, causing the rest of the flock\", type='text')]\n"
     ]
    }
   ],
   "source": [
    "import anthropic\n",
    "\n",
    "client = anthropic.Anthropic(\n",
    "    api_key=\"YOUR API KEY HERE\",\n",
    ")\n",
    "message = client.messages.create(\n",
    "    model=\"claude-sonnet-4-5\",\n",
    "    max_tokens=4096,\n",
    "    messages=[\n",
    "        {\n",
    "            \"role\": \"user\",\n",
    "            \"content\": \"\"\"\n",
    "Please write five stories. Each should be at least 1000 words. Number the words to make sure you don't lose track. Make each story about a different animal.\n",
    "Put them in <story_1>, <story_2>, ... tags\n",
    "\"\"\",\n",
    "        },\n",
    "    ],\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "p29WL9ZkH3OV",
    "outputId": "8cf81b79-8eaa-41b2-e024-28e86b85fb06"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "max_tokens\n"
     ]
    }
   ],
   "source": [
    "print(message.stop_reason)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "G9MhnvnPIs4d"
   },
   "source": [
    "You can see above that Claude stopped sampling because of max_tokens. And you can see below that Claude's message cuts out in the middle of the fifth story."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "6nCnQbBiFV0P",
    "outputId": "a5edc106-c69c-45eb-8782-e7afe787e22a"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<story_1>\n",
      "1. Once upon a time, in the vast expanse of the African savanna, there lived a magnificent lion named Mufasa. 2. His golden mane rippled in the warm breeze, and his piercing amber eyes commanded respect from all who laid eyes upon him. 3. Mufasa was the king of the Pride Lands, a realm teeming with diverse wildlife and lush grasslands.\n",
      "\n",
      "4. As the sun rose each morning, Mufasa would take his daily patrol, surveying his territory with a vigilant gaze. 5. He knew every inch of the Pride Lands, from the towering acacia trees to the winding rivers that sustained life. 6. His powerful roar echoed across the plains, a reminder to all that he was the undisputed ruler of this domain.\n",
      "\n",
      "7. One day, while leading his pride on a hunt, Mufasa noticed a group of hyenas lurking in the shadows. 8. These scavengers were known for their cunning and ruthlessness, and they posed a constant threat to the delicate balance of the Pride Lands. 9. With a flick of his tail, Mufasa signaled his lionesses to remain alert, ready to defend their territory at any cost.\n",
      "\n",
      "10. As the days passed, the hyenas grew bolder, encroaching upon the pride's hunting grounds and terrorizing the other animals. 11. Mufasa knew that he had to take decisive action to protect his kingdom. 12. Gathering his strength, he let out a thunderous roar that shook the very foundations of the savanna, a warning to the hyenas to stay away.\n",
      "\n",
      "13. The hyenas, sensing Mufasa's unwavering determination, retreated for a time, but they were far from defeated. 14. They hatched a devious plan to strike at the heart of the pride, targeting Mufasa's beloved son and heir, Simba. 15. In a harrowing turn of events, Simba found himself caught in a treacherous stampede orchestrated by the hyenas.\n",
      "\n",
      "16. Mufasa, risking his own life, leapt into the chaos to rescue his son. 17. With unparalleled courage and strength, he fought against the raging herd, shielding Simba from harm. 18. But in a tragic twist of fate, Mufasa was swept away by the relentless stampede, his life tragically cut short.\n",
      "\n",
      "19. Simba, devastated by the loss of his father, fled the Pride Lands, leaving the kingdom vulnerable to the machinations of the hyenas and their sinister allies. 20. Years passed, and Mufasa's legacy seemed to fade into the distant memory of the pride.\n",
      "\n",
      "21. However, Mufasa's spirit lived on, guiding Simba from the heavens above. 22. As Simba grew into a formidable lion, he recognized the importance of reclaiming his rightful place as the king of the Pride Lands. 23. With the help of his friends and the wisdom imparted by his father's spirit, Simba embarked on a perilous journey to confront the usurpers and restore balance to the kingdom.\n",
      "\n",
      "24. In an epic battle against the hyenas and their leader, Simba emerged victorious, honoring his father's memory and taking his rightful place as the new king. 25. As he stood atop Pride Rock, his mane catching the golden rays of the setting sun, Simba vowed to uphold the legacy of Mufasa, ruling with wisdom, courage, and unwavering devotion to the Circle of Life.\n",
      "\n",
      "26. From that day forward, the Pride Lands flourished under Simba's just and benevolent reign, and the spirit of Mufasa lived on, guiding future generations of lions to protect and cherish the delicate balance of nature. 27. The legend of the brave and noble Mufasa became etched into the hearts of all who called the Pride Lands their home, a testament to the enduring power of a father's love and sacrifice.\n",
      "</story_1>\n",
      "\n",
      "<story_2>\n",
      "1. Deep within the lush rainforests of the Amazon, a family of spider monkeys swung gracefully from branch to branch, their long tails providing perfect balance. 2. Among them was Chico, a mischievous and curious young monkey whose insatiable appetite for adventure often landed him in precarious situations.\n",
      "\n",
      "3. One morning, as the family foraged for succulent fruits and tender leaves, Chico's attention was drawn to a peculiar sight. 4. A group of brightly colored macaws had descended upon a nearby tree, their vibrant plumage a stark contrast against the emerald canopy. 5. Chico had never seen such magnificent creatures before, and his curiosity got the better of him.\n",
      "\n",
      "6. Ignoring the warnings of his elders, Chico stealthily approached the macaws, his agile movements barely disturbing the foliage around him. 7. As he drew closer, the macaws took notice of the inquisitive monkey and let out a cacophony of squawks and screeches, their wings flapping furiously.\n",
      "\n",
      "8. Undeterred, Chico continued his approach, his mischievous grin widening with every step. 9. However, his bold actions did not go unnoticed by the macaws' leader, a magnificent scarlet macaw with a regal crest atop his head. 10. With a deafening screech, the leader issued a warning, causing the rest of the flock to take flight in a whirlwind of vibrant colors.\n",
      "\n",
      "11. Chico, enthralled by the spectacle, leapt from branch to branch, determined to keep up with the macaws. 12. His family watched in horror as he disappeared into the dense foliage, their calls for him to return lost in the symphony of the rainforest.\n",
      "\n",
      "13. For hours, Chico followed the macaws, his nimble form weaving through the intricate maze of vines and branches. 14. The macaws, sensing a potential threat, led him deeper and deeper into the heart of the rainforest, their flight seemingly effortless.\n",
      "\n",
      "15. As night fell, Chico found himself utterly lost, surrounded by unfamiliar sights and sounds. 16. The calls of his family had long faded, replaced by the eerie hoots of owls and the rustling of unseen creatures lurking in the shadows.\n",
      "\n",
      "17. Exhausted and disoriented, Chico curled up on a sturdy branch, his eyes wide with fear. 18. The night passed slowly, every rustle of leaves sending shivers down his spine. 19. As the first rays of dawn filtered through the canopy, Chico realized the gravity of his situation – he was alone, far from the safety of his family.\n",
      "\n",
      "20. Mustering his courage, Chico set out to retrace his steps, hoping to find familiar landmarks that would guide him home. 21. However, the rainforest proved to be a treacherous labyrinth, each path seeming to lead him further astray.\n",
      "\n",
      "22. Days turned into weeks, and Chico's once vibrant coat grew matted and dull. 23. He learned to forage for sustenance, relying on his sharp senses and agility to survive in the unforgiving wilderness.\n",
      "\n",
      "24. Just when all hope seemed lost, Chico's keen ears picked up a familiar sound – the distinct calls of his family. 25. With renewed determination, he followed the calls, his heart pounding with anticipation.\n",
      "\n",
      "26. Finally, after what seemed like an eternity, Chico emerged from the dense foliage and was greeted by the joyous cries of his kin. 27. Tears of relief streamed down his face as he embraced his family, vowing never again to stray too far from their loving embrace.\n",
      "\n",
      "28. From that day forward, Chico's adventurous spirit was tempered by wisdom and caution. 29. He understood the importance of staying close to his family, for in the vast and unpredictable rainforest, their unity was their greatest strength. 30. And whenever he heard the distant squawks of macaws, Chico would smile, remembering the fateful encounter that had taught him a valuable lesson about the wonders and perils of the Amazon jungle.\n",
      "</story_2>\n",
      "\n",
      "<story_3>\n",
      "1. In the icy depths of the Arctic Ocean, a majestic polar bear named Nanuq prowled the frozen landscape, her powerful paws leaving imprints in the crisp snow. 2. She was the undisputed queen of this unforgiving realm, a master hunter whose keen senses and unwavering determination ensured her survival in the harshest of conditions.\n",
      "\n",
      "3. As the long Arctic winter dragged on, Nanuq's instincts drove her to seek out the best hunting grounds, where the seals congregated in abundance. 4. With each passing day, her hunger grew more insatiable, fueling her relentless pursuit of prey.\n",
      "\n",
      "5. One frigid morning, Nanuq caught the scent of a nearby seal colony, her nostrils flaring with anticipation. 6. Silently, she stalked her way across the vast expanse of ice, her movements fluid and calculated. 7. The seals, oblivious to the approaching danger, basked in the pale Arctic sun, their sleek bodies glistening against the snow.\n",
      "\n",
      "8. Nanuq's powerful jaws began to water as she drew closer, her muscles coiled like springs, ready to strike. 9. In a blur of white fur and razor-sharp claws, she pounced upon her unsuspecting prey, her massive paws pinning the struggling seal to the ice.\n",
      "\n",
      "10. As the hunt unfolded, a curious sight caught Nanuq's eye – a human vessel, cutting through the icy waters with ease. 11. She had encountered humans before, fearless explorers who ventured into her realm with awe and respect. 12. Yet, this time, something felt different, a sense of foreboding that sent a chill down her spine.\n",
      "\n",
      "13. In the days that followed, Nanuq witnessed the human presence grow stronger, their machines and equipment encroaching upon her hunting grounds. 14. The once-pristine ice began to fracture and melt, disrupting the delicate balance of the Arctic ecosystem.\n",
      "\n",
      "15. As the ice retreated, so too did the seals, their traditional breeding and resting grounds disappearing before Nanuq's eyes. 16. Desperate to find sustenance, she ventured farther and farther from her familiar territory, her journey becoming increasingly perilous.\n",
      "\n",
      "17. One fateful day, Nanuq found herself stranded on a rapidly melting ice floe, her powerful body weakened by hunger and exhaustion. 18. The relentless currents tossed her about, separating her from the few remaining seals in the area.\n",
      "\n",
      "19. As the sun dipped below the horizon, Nanuq let out a mournful roar, her cry echoing across the vast, empty expanse of water. 20. In that moment, she understood the grave threat facing her kind – the inexorable march of climate change, fueled by human activity, was slowly eroding the very foundations of her Arctic home.\n",
      "\n",
      "21. With a newfound determination, Nanuq embarked on a perilous journey, swimming for days on end, her powerful strokes cutting through the icy waters. 22. She sought out new hunting grounds, adapting to the ever-changing landscape, her resilience and strength pushing her forward.\n",
      "\n",
      "23. Along the way, she encountered other polar bears, their stories echoing her own struggles. 24. Together, they formed a formidable pack, united in their fight for survival against the forces of nature and humankind.\n",
      "\n",
      "25. As the years passed, Nanuq's legacy lived on through her offspring, each generation bearing witness to the rapid transformation of the Arctic. 26. Their once-frozen kingdom was now a vast, open ocean, dotted with shrinking ice floes and the remnants of a world that was slowly slipping away.\n",
      "\n",
      "27. Yet, even in the face of such adversity, the polar bears remained resolute, their unwavering spirit a testament to the resilience of nature itself. 28. And as Nanuq's descendants gazed out across the ever-changing Arctic, they carried within them the memory of their matriarch, a fierce and noble queen who fought to the very end to preserve the delicate balance of life in the great white north.\n",
      "</story_3>\n",
      "\n",
      "<story_4>\n",
      "1. In the heart of the lush Australian outback, a family of kangaroos grazed peacefully on the sun-drenched grasslands. 2. Among them was Kiki, a spirited young joey whose boundless energy and insatiable curiosity often led her into mischievous escapades.\n",
      "\n",
      "3. One sultry afternoon, as the kangaroo mob rested in the shade of a towering gum tree, Kiki's attention was captured by a strange, slithering creature. 4. Its iridescent scales glistened in the dappled sunlight, and its forked tongue flicked in and out, tasting the warm air.\n",
      "\n",
      "5. Ignoring the warnings of her elders, Kiki hopped closer, her large feet kicking up clouds of red dust. 6. The snake, sensing the presence of the inquisitive joey, coiled and hissed, its menacing fangs glinting in the sunlight.\n",
      "\n",
      "7. Undeterred, Kiki continued her approach, her black button nose twitching with excitement. 8. Just as she reached out a paw to touch the serpent, her mother intervened, scooping her up and bounding away to safety.\n",
      "\n",
      "9. Kiki's curiosity, however, remained unabated. 10. In the days that followed, she became obsessed with the enigmatic snake, spending hours watching it from a safe distance, studying its every move.\n",
      "\n",
      "11. As the seasons changed and the harsh Australian summer gave way to a cool autumn, Kiki's fascination only grew stronger. 12. She ventured farther and farther from her family, her explorations taking her deep into the rugged outback terrain.\n",
      "\n",
      "13. One crisp morning, Kiki found herself face-to-face with the snake once more, its hypnotic gaze holding her in place. 14. With a flick of its tongue, the serpent began to slither towards her, its powerful coils rippling across the dusty ground.\n",
      "\n",
      "15. Kiki's heart raced, her instincts torn between fight and flight. 16. Just as the snake lunged, a booming voice echoed through the bush, startling them both. 17. It was Kiki's father, a powerful buck whose imposing presence commanded respect.\n",
      "\n",
      "18. With a swift kick of his powerful hind legs, he sent the snake recoiling, its body coiling in defensive strikes. 19. Kiki watched in awe as her father stood his ground, his muscles rippling beneath his sleek coat.\n",
      "\n",
      "20. In that moment, Kiki understood the true nature of the outback – a land of both beauty and danger, where survival depended on respect for the delicate balance of nature.\n",
      "\n",
      "21. As the years passed, Kiki's adventurous spirit never waned, but her encounters with the snake had instilled in her a deeper appreciation for the power and resilience of the Australian wildlife.\n",
      "\n",
      "22. She became a skilled hunter and navigator, her powerful legs propelling her across the vast, sun-baked plains with ease. 23. Her keen senses alerted her to the slightest movements, allowing her to avoid potential threats and seek out the best grazing grounds.\n",
      "\n",
      "24. And whenever she caught a glimpse of that familiar serpentine form, Kiki would pause, her eyes filled with a mixture of fascination and respect. 25. For in the harsh and unforgiving outback, every creature played a vital role in the intricate web of life.\n",
      "\n",
      "26. As the seasons cycled and the kangaroo mob grew, Kiki passed on her hard-earned wisdom to the next generation, teaching them the importance of vigilance and respect for the land that sustained them.\n",
      "\n",
      "27. And on moonlit nights, when the outback came alive with the haunting calls of dingoes and the rustle of nocturnal creatures, Kiki would gaze up at the brilliant tapestry of stars, her heart swelling with gratitude for the magnificent world she called home.\n",
      "</story_4>\n",
      "\n",
      "<story_5>\n",
      "1. Deep within the lush, emerald canopy of the Amazon rainforest, a vibrant family of toucans made their home. 2. Among them was Zico, a mischievous and curious young toucan whose insatiable appetite for adventure often led him astray.\n",
      "\n",
      "3. One morning, as the family foraged for succulent fruits and juicy insects, Zico's attention was drawn to a peculiar sight. 4. A group of colorful macaws had descended upon a nearby tree, their vibrant plumage a stark contrast against the verdant foliage.\n",
      "\n",
      "5. Zico had never encountered such magnificent creatures before, and his curiosity piqued. 6. Ignoring the warnings of his elders, he took flight, his powerful wings carrying him closer to the macaws.\n",
      "\n",
      "7. As he drew nearer, the macaws took notice of the inquisitive toucan, letting out a cacophony of squawks and screeches. 8. Undeterred, Zico continued his approach, his distinctive beak bobbing with every flap of his wings.\n",
      "\n",
      "9. Suddenly, the leader of the macaw flock, a magnificent scarlet macaw with a regal crest, took to the air, its wings beating furiously. 10. With a deafening screech, the leader issued a warning, causing the rest of the flock\n"
     ]
    }
   ],
   "source": [
    "print(message.content[0].text)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "BG7pJczyI8N5"
   },
   "source": [
    "Solution? We put the partially completed response, which was cut off in its prime, in an Assistant message to Claude, who then continues sampling and completes the story. You can see that Claude seamlessly picks up in mid-sentence."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "0LfGXp1wGrjU",
    "outputId": "ff9aefa5-5a39-4285-ef07-08280e1edfb1"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " to disperse in a whirlwind of vibrant colors.\n",
      "\n",
      "11. Zico, enthralled by the spectacle, followed the macaws deeper into the heart of the rainforest, weaving through the intricate maze of vines and branches. 12. His family's calls for him to return faded into the distance, lost in the symphony of the jungle.\n",
      "\n",
      "13. For hours, Zico trailed the macaws, his nimble form navigating the treacherous terrain with ease. 14. The macaws, sensing a potential threat, led him on a winding path, their flight seemingly effortless.\n",
      "\n",
      "15. As dusk fell, Zico found himself utterly lost, surrounded by unfamiliar sights and sounds. 16. The calls of his family had long faded, replaced by the eerie hoots of owls and the rustling of unseen creatures lurking in the shadows.\n",
      "\n",
      "17. Exhausted and disoriented, Zico alighted on a sturdy branch, his eyes wide with fear. 18. The night passed slowly, every rustle of leaves sending shivers down his spine.\n",
      "\n",
      "19. As the first rays of dawn filtered through the canopy, Zico realized the gravity of his situation – he was alone, far from the safety of his family's nest.\n",
      "\n",
      "20. Mustering his courage, Zico took to the air once more, his keen eyes scanning the landscape for familiar landmarks. 21. However, the rainforest proved to be a treacherous labyrinth, each path seeming to lead him further astray.\n",
      "\n",
      "22. Days turned into weeks, and Zico's once-vibrant feathers grew dull and matted. 23. He learned to forage for sustenance, relying on his sharp senses and agility to survive in the unforgiving wilderness.\n",
      "\n",
      "24. Just when all hope seemed lost, Zico's keen ears picked up a familiar sound – the distinct calls of his family. 25. With renewed determination, he followed the calls, his heart pounding with anticipation.\n",
      "\n",
      "26. Finally, after what seemed like an eternity, Zico emerged from the dense foliage and was greeted by the joyous cries of his kin. 27. Tears of relief streamed down his face as he embraced his family, vowing never again to stray too far from their loving embrace.\n",
      "\n",
      "28. From that day forward, Zico's adventurous spirit was tempered by wisdom and caution. 29. He understood the importance of staying close to his family, for in the vast and unpredictable rainforest, their unity was their greatest strength.\n",
      "\n",
      "30. And whenever he heard the distant squawks of macaws, Zico would smile, remembering the fateful encounter that had taught him a valuable lesson about the wonders and perils of the Amazon jungle.\n",
      "</story_5>\n"
     ]
    }
   ],
   "source": [
    "message2 = client.messages.create(\n",
    "    model=\"claude-sonnet-4-5\",\n",
    "    max_tokens=4096,\n",
    "    messages=[\n",
    "        {\n",
    "            \"role\": \"user\",\n",
    "            \"content\": \"\"\"\n",
    "Please write five stories. Each should be at least 1000 words. Number the words to make sure you don't lose track. Make each story about a different animal.\n",
    "Put them in <story_1>, <story_2>, ... tags\n",
    "\"\"\",\n",
    "        },\n",
    "        {\n",
    "            \"role\": \"assistant\",\n",
    "            \"content\": message.content[0].text,  # The text of Claude's partially completed message.\n",
    "        },\n",
    "    ],\n",
    ")\n",
    "print(message2.content[0].text)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "yUxKwrfRJiBw"
   },
   "source": [
    "Please be advised that this approach unfortunately entails being \"double-charged\" for reading the input tokens in your prompt and single-charged for reading the output tokens of Claude's 4096-token response as input tokens. However, they will not be double-charged as output tokens."
   ]
  }
 ],
 "metadata": {
  "colab": {
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3",
   "name": "python3"
  },
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
